package com.jason.redCherry.action;


import com.alibaba.fastjson.JSON;
import com.jason.redCherry.common.Global;
import com.jason.redCherry.common.StatusCodeManager;
import com.jason.redCherry.common.ToolUtil;
import com.jason.redCherry.dao.ConfigMapper;
import com.jason.redCherry.dao.GuestMapper;
import com.jason.redCherry.dao.JSMapper;
import com.jason.redCherry.entity.Config;
import com.jason.redCherry.entity.Guest;
import com.jason.redCherry.entity.JS;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jason on 2016/9/5.
 */
public class MainAction extends BasicAction {

    private Log log = LogFactory.getLog(MainAction.class);
    @Resource
    private ConfigMapper configMapper;
    @Resource
    private JSMapper jsMapper;
    @Resource
    private GuestMapper guestMapper;

    private String version;
    private String userId;
    private String pwd;
    private String mac;

    //更新数据
    public void update() {
        log.info("update==>userId=" + userId + ",pwd=" + pwd + ",version=" + version + ",mac=" + mac);
        Guest guest = guestMapper.selectByPrimaryKey(userId);
        Date now = new Date();

        //校验用户
        if (guest == null || !guest.getPwd().equals(pwd)) {
            resultMsg(StatusCodeManager.ERROR_PWD);
            return;
        }
        //账户不可用
        if (guest.getStatus() == 1) {
            resultMsg(StatusCodeManager.ERROR_CANT_USE);
            return;
        }

        Map<String, Object> ext = new HashMap<String, Object>();
        ext.put("version", version);
        ext.put("untilTime", ToolUtil.getDateStr(guest.getUntilTime()));
        ext.put("hasUpdate", false);
        //检查用户使用期限
        if (guest.getUntilTime() != null && guest.getUntilTime().compareTo(now) < 0) {
            resultMsg(StatusCodeManager.ERROR_TIMT_UNTIL, ext);
            log.info("ERROR_TIMT_UNTIL,time=" + guest.getUntilTime());
            return;
        }
        //检查mac地址
        if (!guest.getMacAddress().equals(mac)) {
            resultMsg(StatusCodeManager.ERROR_MAC, ext);
            log.info("ERROR_MAC,mac=" + guest.getMacAddress());
            return;
        }
        //检查需不需要更新
        Config appVersion = configMapper.selectByKey(Global.CONFIG_APP_VERSION);
        if (appVersion != null && appVersion.getSysVal().equals(version)) {
            log.info("NO_NEED_UPDATE");
            resultMsg(StatusCodeManager.SUCCESS, ext);
            return;
        }
        ext.put("version", appVersion.getSysVal());
        //查询需要更新的内容
        Config config = configMapper.selectByKey(Global.CONFIG_RULE);
        if (config == null) {
            log.info("ERROR_UPDATE");
            resultMsg(StatusCodeManager.ERROR_UPDATE, ext);
            return;
        }
        ext.put("hasUpdate", true);
        String rule = config.getSysJson();
        if (rule != null && !"".equals(rule)) {
            ext.put("rule", JSON.parseObject(rule));
        }
        List<JS> jsList = jsMapper.selectAll();
        ext.put("jsList", jsList);
        log.info("SUCCESS");
        resultMsg(StatusCodeManager.SUCCESS, ext);
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }
}
